//
//  RegistView.m
//  LoginTest
//
//  Created by 战立军 on 2017/3/3.
//  Copyright © 2017年 战立军. All rights reserved.
//

#import "RegistView.h"

@interface RegistView ()<UITextFieldDelegate>
{
    UITextField *_userPhone;//手机号
    
    UITextField *_userNickName;//昵称
    
    UITextField *_code;//验证码
    
    NSTimer *_tiemr;//定时器
    
    int _timeCount;
    
    UIButton *_codeBtn;//获取验证码
    
    UITextField *_userPwd;//密码
    
    UITextField *_userPwdAgain;//验证密码
    
//    UIButton *_userHeadImage;//头像
    
    UIButton *_sureButton;
    
    CGFloat _myheight;
    CGFloat _headheight;
}
@end

@implementation RegistView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addContentView];
    }
    
    return self;
}

- (void)addContentView
{
    
    _myheight = 50;
    _headheight = 64;
    if (IS_IPHONE_5) {
        _myheight = 50;
        _headheight = 64;
    }
    
    if (IS_IPHONE_6) {
        _myheight = 50;
        _headheight = 64;
    }
    if (IS_IPHONE_6P) {
        
        _myheight = 65;
        _headheight = 64;
    }

    //背景色
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIUTils colorFromHexCode:@"0489c2"].CGColor,(__bridge id)[UIUTils colorFromHexCode:@"009c93"].CGColor];
    gradientLayer.locations = @[@0.3,@1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(0, 1.0);
    gradientLayer.frame = self.bounds;
    [self.layer insertSublayer:gradientLayer atIndex:0];
    
    //手机号
        _userPhone = [[UITextField alloc] init];
        _userPhone.keyboardType = UIKeyboardTypeNumberPad;
        UIImageView *userPhoneView  =[self InputBoxTitle:@"手机号" placehold:@"请输入用户名" weithFram:CGRectMake(20, _headheight, [UIUTils getWindowWidth]-20*2, _myheight) textField:_userPhone isSecure:NO];
        [self addSubview:userPhoneView];
    //验证码
    
    _code = [[UITextField alloc] init];
    _code.keyboardType = UIKeyboardTypeNumberPad;
    UIImageView *usercodeView  =[self InputBoxTitle:@"验证码" placehold:@"请输入验证码" weithFram:CGRectMake(20, CGRectGetMaxY(userPhoneView.frame)+10, ([UIUTils getWindowWidth]-20*2)/4.0*3, _myheight) textField:_code isSecure:NO];
    [self addSubview:usercodeView];

    _codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _codeBtn.frame = CGRectMake(CGRectGetMaxX(usercodeView.frame)-2, CGRectGetMaxY(userPhoneView.frame)+10, ([UIUTils getWindowWidth]-20*2)/4.0+2, _myheight/4.0*3.0);
    _codeBtn.backgroundColor = [UIUTils colorFromHexCode:@"#00aaee"];
    [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _codeBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    _codeBtn.layer.cornerRadius = 2;
    [_codeBtn addTarget:self action:@selector(codebuttonPress) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_codeBtn];
    
    //密码
    
    _userPwd = [[UITextField alloc] init];
//    _code.keyboardType = UIKeyboardTypeNumberPad;
    UIImageView *userpwdView  =[self InputBoxTitle:@"密码" placehold:@"请输入密码" weithFram:CGRectMake(20, CGRectGetMaxY(usercodeView.frame)+10, ([UIUTils getWindowWidth]-20*2), _myheight) textField:_userPwd isSecure:YES];
    [self addSubview:userpwdView];
    
    //验证密码
    
    _userPwdAgain = [[UITextField alloc] init];
    //    _code.keyboardType = UIKeyboardTypeNumberPad;
    UIImageView *userpwdagainView  =[self InputBoxTitle:@"验证密码" placehold:@"请再次输入密码" weithFram:CGRectMake(20, CGRectGetMaxY(userpwdView.frame)+10, ([UIUTils getWindowWidth]-20*2), _myheight) textField:_userPwdAgain isSecure:YES];
    [self addSubview:userpwdagainView];

    
    //昵称
    
    _userNickName = [[UITextField alloc] init];
    //    _code.keyboardType = UIKeyboardTypeNumberPad;
    UIImageView *usernicknameView  =[self InputBoxTitle:@"昵称" placehold:@"请输入昵称" weithFram:CGRectMake(20, CGRectGetMaxY(userpwdagainView.frame)+10, ([UIUTils getWindowWidth]-20*2)/4.0*3, _myheight) textField:_userNickName isSecure:YES];
    [self addSubview:usernicknameView];
    
    //头像
    
    _userHeadImage = [UIButton buttonWithType:UIButtonTypeCustom];
    _userHeadImage.frame = CGRectMake(CGRectGetMaxX(usernicknameView.frame)+(([UIUTils getWindowWidth]-20*2)/4.0-_myheight/4.0*3.0)/2.0, CGRectGetMaxY(userpwdagainView.frame)+10, _myheight/4.0*3.0, _myheight/4.0*3.0);
    [_userHeadImage setImage:[UIImage imageNamed:@"upload"] forState:UIControlStateNormal];
    [_userHeadImage addTarget:self action:@selector(headImageButtonPress) forControlEvents:UIControlEventTouchUpInside];
//    _userHeadImage.backgroundColor = [UIColor redColor];
    [self addSubview:_userHeadImage];
    
    
    
    //注册
    
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureButton.frame = CGRectMake(20, CGRectGetMaxY(usernicknameView.frame)+20, [UIUTils getWindowWidth]-20*2, _myheight-10);
    _sureButton.backgroundColor = [UIUTils colorFromHexCode:@"#eb6065"];
    [_sureButton setTitle:@"注册" forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(registButtonPress) forControlEvents:UIControlEventTouchUpInside];
    _sureButton.layer.cornerRadius = 10;
    [self addSubview:_sureButton];
    
    //返回
    
    UILabel *_loginLabe = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(_sureButton.frame)+10, ([UIUTils getWindowWidth]-30*2)/2.0, _myheight-10)];
//    _loginLabe.backgroundColor = [UIColor redColor];
    _loginLabe.text = @"你已有一个账号？";
    _loginLabe.textColor = [UIUTils colorFromHexCode:@"#a6a6a6"];
    _loginLabe.adjustsFontSizeToFitWidth = YES;
    [self addSubview:_loginLabe];
    
    UIButton *_backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _backbtn.frame = CGRectMake(CGRectGetMaxX(_loginLabe.frame), CGRectGetMaxY(_sureButton.frame)+10, ([UIUTils getWindowWidth]-30*2)/2.0, _myheight-10);
//    _backbtn.backgroundColor = [UIColor yellowColor];
    [_backbtn setTitle:@"立即登录" forState:UIControlStateNormal];
    [_backbtn addTarget:self action:@selector(backButtonPress) forControlEvents:UIControlEventTouchUpInside];
    _backbtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    [self addSubview:_backbtn];
}

#pragma mark - 输入框格式
/**
 *  创建本页面使用的输入框
 *
 *  @param title     输入项目名称
 *  @param placehold 输入提示
 *  @param frame     大小位置
 */

- (UIImageView *)InputBoxTitle:(NSString*)title placehold:(NSString *)placehold weithFram:(CGRect)frame textField:(UITextField *)textField isSecure:(BOOL)bools
{
    //背景图片
    float boxH = frame.size.height/4 *3;//输入框的高度
    
    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.origin.x,frame.origin.y,frame.size.width,boxH)];
    backView.userInteractionEnabled = YES;
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.cornerRadius = 3;
    
    
    
    //    backView.image = [UIImage imageNamed:@"input-box"];
    //    [view addSubview:backView];
    
    //输入项目
    
    UILabel *invalabe = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 150, boxH)];
    invalabe.text = title;
    //    invalabe.adjustsFontSizeToFitWidth = YES;
    CGRect newRect= [invalabe textRectForBounds:CGRectMake(20, 0, 150, 40) limitedToNumberOfLines:1];
    [invalabe setFrame:CGRectMake(10, 0,newRect.size.width, boxH)];
    invalabe.textColor = [UIUTils colorFromHexCode:@"#19A5EE"];
    [backView addSubview:invalabe];
    
    textField.frame =CGRectMake(CGRectGetMaxX(invalabe.frame)+10, 0, frame.size.width-newRect.size.width-20, boxH);
    //    textField.backgroundColor = [UIColor redColor];
    if (bools) {//密文
        [textField setSecureTextEntry:YES];
    }
    textField.placeholder = placehold;
    textField.font = [UIFont systemFontOfSize:15];
    textField.delegate = self;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [backView addSubview:textField];
    
    return backView;
}

- (void)timesycle
{
//    _timeCount--;
    
    NSLog(@"####%d",_timeCount);
    
    if (_timeCount>1)
    {
         _timeCount--;
        NSString *title = [NSString stringWithFormat:@"%d秒",_timeCount];
        [_codeBtn setTitle:title forState:UIControlStateNormal];
    }
    else
    {
        [_tiemr invalidate];
        _codeBtn.backgroundColor = [UIUTils colorFromHexCode:@"#00aaee"];
        [_codeBtn setTitle:@"重新获取密码" forState:UIControlStateNormal];
        _codeBtn.enabled = YES;
    }
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}

#pragma mark UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)codebuttonPress
{
    [self endEditing:YES];
    //验证手机号
    //获取短信
    
    if ([UIUTils isPhoneNumber:_userPhone.text])
    {
        [self getSmsWithPhone:_userPhone.text];
    }
    else
    {
        [UIUTils showReminderMBProgressHUDWithTitle:@"手机号格式不正确" view:self];
        return;
    }
    
    //修改button
    _codeBtn.enabled = NO;
    [_codeBtn setBackgroundColor:[UIUTils colorFromHexCode:@"a6a6a6"]];
    [_codeBtn setTitle:@"50秒" forState:UIControlStateNormal];
    
    //开启定时器
    _timeCount = 50;
    [_tiemr invalidate];
    _tiemr = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timesycle) userInfo:nil repeats:YES];
}

- (void)backButtonPress
{
    [self endEditing:YES];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(registViewloginButtonpress)]) {
        [_tiemr invalidate];
        [_tiemr setFireDate:[NSDate distantFuture]];
        _tiemr = nil;
        [self.delegate registViewloginButtonpress];
    }
}

- (void)registButtonPress
{
    [self endEditing: YES];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(registViewRegistButtonpress)]) {
        [self.delegate registViewRegistButtonpress];
    }
}

- (void)headImageButtonPress
{
    [self endEditing: YES];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(registViewuserheadButtonpress)]) {
        [self.delegate registViewuserheadButtonpress];
    }
}

- (void)getSmsWithPhone:(NSString *)phone
{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(registViewgetsmsButtonpressWithPhone:)]) {
        [self.delegate registViewgetsmsButtonpressWithPhone:phone];
    }
}


@end
